/** 计算函数
 * @ 标  识: hchhIOT
 * @ 作  者: 阿拉丁矿工 黄传鸿 <hchhyzc@foxmail.com>
 */

/* ================================================================================================ */
// 🍀引入
/* ================================================================================================ */
import { v4 as uuid } from "uuid"
import * as XLSX from "xlsx"


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// 🐸 图元端定义引入
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
import { CoordinateType } from '@/defines'


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// 🐸 引入存储
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
import { useCanvasStore } from "@/store/canvasStore"; // 画布
import { useProjectStore } from "@/store/projectStore"; // 工程

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// 🐸 使用存储
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
const canvasStore = useCanvasStore(); // 画布
const projectStore = useProjectStore(); // 工程

/* ================================================================================================ */
//                                函数
/* ================================================================================================ */
//

/**———————————————————————————————————————————————————————————————————————————————————————————————— */
//                                ★ 计算
/**———————————————————————————————————————————————————————————————————————————————————————————————— */
/** 获取UUID */
export const onGetUUID = (): string => {
  // console return (((1 + Math.random()) * 0x100000000) | 0).toString(16).substring(1);.log("㊣ 公共函数-获取UUID")
  // let strUUID = uuid().replace(/-/g, ""); // 去掉-字符，使用空格代替
  // return String(strUUID);
 
}

/**———————————————————————————————————————————————————————————————————————————————————————————————— */
//                                ★ 计算光标点
/**———————————————————————————————————————————————————————————————————————————————————————————————— */
/** 计算光标点
 * @参数_MousePoint 鼠标点
 * @返回 0_窗体坐标[x,y];
 * @返回 2_真实坐标[x,y];
 */
export const onCalcCursor = (x: number, y: number) => {
  // console.log("㊣ 公共函数-计算光标点");

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 转储 建立参数
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  /** 获取-画布缩放 */
  let canvasScale = Number(canvasStore.canvasAttr.canvasScale); //画布[0]画布缩放 canvasScale
  /** 转换坐标 */
  let ConvertCoordinate = {
    x: x * (1 / canvasScale),
    y: y * (1 / canvasScale),
  };
  // console.log("转换坐标:" );
  /** 光标捕捉x */
  let CatchX = ConvertCoordinate.x;
  /** 光标捕捉y */
  let CatchY = ConvertCoordinate.y;
  /** 捕捉格栅宽度 */
  let catchWidthVal = 1;
  /** 捕捉格栅高度 */
  let catchHeightVal = 1;
  /** 捕捉区域 */
  let catchRange = Number(canvasStore.canvasAttr.catchRange);
  /** 格栅宽度 */
  let gridWidth = Number(canvasStore.canvasAttr.gridWidth);
  /** 格栅高度 */
  let gridHeight = Number(canvasStore.canvasAttr.gridHeight);
  /** 捕捉状态 */
  let isCatch = Boolean(canvasStore.canvasAttr.isCatch);
  /** 画布宽度 */
  let canvasWidth = Number(canvasStore.canvasAttr.canvasWidth);
  /** 画布高度 */
  let canvasHeight = Number(canvasStore.canvasAttr.canvasHeight);
  /** 格栅间距 */
  let gridSpace = Number(canvasStore.canvasAttr.gridSpace);
  /** 格栅模式选项:1-无格栅;2-点格栅;3-方格; */
  let gridModel = Number(canvasStore.canvasAttr.gridModel);
  /** 格栅样式选项:1-像素尺寸;2-均分画布;3-按宽高均分画布;*/
  let gridStyle = Number(canvasStore.canvasAttr.gridStyle);

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 格栅模式选项:1-无格栅;
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  if (gridModel === 1/*无格栅*/) {
    catchWidthVal = catchRange;
    catchHeightVal = catchRange;
  }

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 格栅模式选项:2-点格栅;
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  if (gridModel === 2/*点格栅*/) {
    catchWidthVal = gridSpace;
    catchHeightVal = gridSpace;
  }

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 格栅模式选项:3-方格;
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  if (gridModel === 3/*方格*/) {

    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    // 🐸 ★ 格栅模式选项:3-方格;|格栅样式选项:1-像素尺寸;
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    if (gridStyle === 1/*像素尺寸*/) {
      catchWidthVal = gridSpace;
      catchHeightVal = gridSpace;
    }

    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    // 🐸 ★ 格栅模式选项:3-方格;|格栅样式选项:2-均分画布;
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    if (gridStyle === 2/*均分画布*/) {
      catchWidthVal = canvasWidth / gridSpace;
      catchHeightVal = canvasHeight / gridSpace;
    }

    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    // 🐸 ★ 格栅模式选项:3-方格;|格栅样式选项:3-按宽高均分画布;
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    if (gridStyle === 3/*按宽高均分画布*/) {
      catchWidthVal = gridWidth;
      catchHeightVal = gridHeight;
    }
  }

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 捕捉
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  /**★ 判断光标捕捉状态 */
  if (isCatch) {

    try {
      //捕捉X
      let GridX = String(ConvertCoordinate.x / catchWidthVal);
      // console.log(`${GridX}`);
      let SplitX = GridX.split(".");
      // console.log(`SplitX=${SplitX}`);
      let SliceX = Number(SplitX[1].slice(0, 1));
      // console.log(`SliceX==${SliceX}`);
      if (SliceX > 5) {
        CatchX = Number(SplitX[0]) * catchWidthVal + catchWidthVal;
      } else {
        CatchX = Number(SplitX[0]) * catchWidthVal;
      }

      //捕捉Y
      let GridY = String(ConvertCoordinate.y / catchHeightVal);
      // console.log(`${GridX}`);
      let SplitY = GridY.split(".");
      // console.log(`SplitX=${SplitX}`);
      let SliceY = Number(SplitY[1].slice(0, 1));
      // console.log(`SliceY==${SliceY}`);
      if (SliceY > 5) {
        CatchY = Number(SplitY[0]) * catchHeightVal + catchHeightVal;
      } else {
        CatchY = Number(SplitY[0]) * catchHeightVal;
      }
    } catch (error) {
      // console.log(error);
    }
  } else {
    CatchX = ConvertCoordinate.x;
    CatchY = ConvertCoordinate.y;
  }

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 精度
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  let useAccuracy = String(canvasStore.canvasAttr.pageAccuracy);
  let AccuracyCoordinate = { x: 0, y: 0 };
  /**★ 精度 */
  if (useAccuracy === "0级") {
    AccuracyCoordinate = {
      x: parseInt(String(CatchX)),
      y: parseInt(String(CatchY)),
    };
  }
  if (useAccuracy === "1级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(1)),
      y: Number(CatchY.toFixed(1)),
    };
  }
  if (useAccuracy === "2级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(2)),
      y: Number(CatchY.toFixed(2)),
    };
  }
  if (useAccuracy === "3级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(3)),
      y: Number(CatchY.toFixed(3)),
    };
  }
  if (useAccuracy === "4级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(4)),
      y: Number(CatchY.toFixed(4)),
    };
  }
  if (useAccuracy === "5级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(5)),
      y: Number(CatchY.toFixed(5)),
    };
  }
  if (useAccuracy === "6级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(6)),
      y: Number(CatchY.toFixed(6)),
    };
  }
  if (useAccuracy === "7级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(7)),
      y: Number(CatchY.toFixed(7)),
    };
  }
  if (useAccuracy === "8级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(8)),
      y: Number(CatchY.toFixed(8)),
    };
  }

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 真实坐标
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  canvasStore.coordinate.x = AccuracyCoordinate.x;
  canvasStore.coordinate.y = AccuracyCoordinate.y;
}

/** 计算移动光标点
 * @参数_MousePoint 鼠标点
 * @返回 0_窗体坐标[x,y];
 * @返回 2_真实坐标[x,y];
 */
export const onCalcMoveCursor = (x: number, y: number) => {
  // console.log("㊣ 公共函数-计算光标点");

  /** 获取-画布缩放 */
  let canvasScale = Number(canvasStore.canvasAttr.canvasScale); //画布[0]画布缩放 canvasScale

  /** 转换坐标 */
  let ConvertCoordinate = {
    x: x * (1 / canvasScale),
    y: y * (1 / canvasScale),
  };
  // console.log("转换坐标:" );

  //--------------------------------------------------|捕捉
  /**★ 光标捕捉 */
  let CatchX = ConvertCoordinate.x;
  let CatchY = ConvertCoordinate.y;

  let gridWidth = Number(canvasStore.canvasAttr.catchRange);
  let gridHeight = Number(canvasStore.canvasAttr.catchRange);

  let isCatch = Boolean(canvasStore.canvasAttr.isCatch);

  /**★ 判断光标捕捉状态 */
  if (isCatch) {
    // if (CoordinateFrame.x < 0 || CoordinateFrame.y < 0) {
    // console.log("光标捕捉");
    // console.log(`格栅宽度==${projectStore.FrameInfo.gridWidth}`);
    try {
      //捕捉X
      let GridX = String(ConvertCoordinate.x / gridWidth);
      // console.log(`${GridX}`);
      let SplitX = GridX.split(".");
      // console.log(`SplitX=${SplitX}`);
      let SliceX = Number(SplitX[1].slice(0, 1));
      // console.log(`SliceX==${SliceX}`);
      if (SliceX > 5) {
        CatchX = Number(SplitX[0]) * gridWidth + gridWidth;
      } else {
        CatchX = Number(SplitX[0]) * gridWidth;
      }

      //捕捉Y
      let GridY = String(ConvertCoordinate.y / gridHeight);
      // console.log(`${GridX}`);
      let SplitY = GridY.split(".");
      // console.log(`SplitX=${SplitX}`);
      let SliceY = Number(SplitY[1].slice(0, 1));
      // console.log(`SliceY==${SliceY}`);
      if (SliceY > 5) {
        CatchY = Number(SplitY[0]) * gridHeight + gridHeight;
      } else {
        CatchY = Number(SplitY[0]) * gridHeight;
      }
    } catch (error) {
      // console.log(error);
    }
  } else {
    CatchX = ConvertCoordinate.x;
    CatchY = ConvertCoordinate.y;
  }

  //--------------------------------------------------|精度
  let useAccuracy = String(canvasStore.canvasAttr.pageAccuracy);
  let AccuracyCoordinate = { x: 0, y: 0 };
  /**★ 精度 */
  if (useAccuracy === "0级") {
    AccuracyCoordinate = {
      x: parseInt(String(CatchX)),
      y: parseInt(String(CatchY)),
    };
  }
  if (useAccuracy === "1级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(1)),
      y: Number(CatchY.toFixed(1)),
    };
  }
  if (useAccuracy === "2级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(2)),
      y: Number(CatchY.toFixed(2)),
    };
  }
  if (useAccuracy === "3级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(3)),
      y: Number(CatchY.toFixed(3)),
    };
  }
  if (useAccuracy === "4级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(4)),
      y: Number(CatchY.toFixed(4)),
    };
  }
  if (useAccuracy === "5级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(5)),
      y: Number(CatchY.toFixed(5)),
    };
  }
  if (useAccuracy === "6级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(6)),
      y: Number(CatchY.toFixed(6)),
    };
  }
  if (useAccuracy === "7级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(7)),
      y: Number(CatchY.toFixed(7)),
    };
  }
  if (useAccuracy === "8级") {
    AccuracyCoordinate = {
      x: Number(CatchX.toFixed(8)),
      y: Number(CatchY.toFixed(8)),
    };
  }
  // 移动坐标
  canvasStore.moveCoordinate.x = AccuracyCoordinate.x;
  canvasStore.moveCoordinate.y = AccuracyCoordinate.y;
}

/** 获取滑块精度 */
export const onSliderAccuracy = () => {
  // console.log("获取页面精度")

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 转储
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  /** 最大页面尺寸 */
  let MaxCanvasSize = 0
  // 画布[2]画布宽度 canvasWidth
  let canvasWidth = Number(canvasStore.canvasAttr.canvasWidth)
  // console.log(canvasWidth)
  // 画布[3]画布高度 canvasHeight
  let canvasHeight = Number(canvasStore.canvasAttr.canvasHeight)
  // console.log(canvasHeight)
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 计算最大页面尺寸
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // console.log("计算最大页面尺寸")
  if (canvasWidth >= canvasHeight) {
    MaxCanvasSize = canvasWidth
  }
  if (canvasHeight >= canvasWidth) {
    MaxCanvasSize = canvasHeight
  }
  // console.log(MaxCanvasSize)

  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 计算 最大值 最小值
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // console.log("计算 最大值 最小值")
  canvasStore.sliderMax = MaxCanvasSize * 10
  // console.log(canvasStore.sliderMax)
  canvasStore.sliderMin = Number('-' + canvasStore.sliderMax)
  // console.log(canvasStore.sliderMin)
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 设置精度
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 页面[3]页面精度 pageAccuracy
  let useAccuracy = String(canvasStore.canvasAttr.pageAccuracy)
  /**使用精度 */
  if (useAccuracy === "0级") {
    canvasStore.sliderStep = 1
  }
  if (useAccuracy === "1级") {
    canvasStore.sliderStep = 0.1
  }
  if (useAccuracy === "2级") {
    canvasStore.sliderStep = 0.01
  }
  if (useAccuracy === "3级") {
    canvasStore.sliderStep = 0.001
  }
  if (useAccuracy === "4级") {
    canvasStore.sliderStep = 0.0001
  }
  if (useAccuracy === "5级") {
    canvasStore.sliderStep = 0.00001
  }
  if (useAccuracy === "6级") {
    canvasStore.sliderStep = 0.000001
  }
  if (useAccuracy === "7级") {
    canvasStore.sliderStep = 0.0000001
  }
  if (useAccuracy === "8级") {
    canvasStore.sliderStep = 0.00000001
  }
}

/** 获取元素计数 */
export const onGetElementCont = (Key: string): number => {
  // console.log("㊣ 公共函数-获取元素计数");
  let res = 0;
  let elementCont = projectStore.elementCont;
  for (let index = 0; index < elementCont.length; index++) {
    const element = elementCont[index];
    if (element.key === Key) {
      res = Number(element.val);
    }
  }
  // console.log(`key==${Key};res==${res};`);
  return res;
}

/** 设置元素计数 */
export const onSetElementCont = (Key: string, newVal: number): number => {
  // console.log("㊣ 公共函数-设置元素计数");
  // console.log(`key==${Key};val==${newVal};`);
  /** ◆ 遍历元素计数 */
  let elementCont = projectStore.elementCont;
  for (let index = 0; index < elementCont.length; index++) {
    const element = elementCont[index];
    if (element.key === Key) {
      element.val = Number(newVal);
    }
  }
  return newVal;
}

/** 获取位置 
 * @参数 StartPoint 开始点    {x,y}
 * @参数 EndPoint   结束点    {x,y}
 * @返回 Places   位置   {tl tc tr l c r bl bc br}
*/
export const onGetPlaces = (StartPoint: CoordinateType, EndPoint: CoordinateType): any => {
  // console.log("线计算函数-获取位置");

  /** 上左点 */
  let TopLeftPoint = {
    x: Number(StartPoint.x),
    y: Number(StartPoint.y),
  }

  /** 上中点 */
  let TopCenterPoint = {
    x: Number((EndPoint.x - StartPoint.x) / 2),
    y: Number(StartPoint.y),
  }

  /** 上右点 */
  let TopRightPoint = {
    x: Number(EndPoint.x),
    y: Number(StartPoint.y),
  }

  /** 左点 */
  let LeftPoint = {
    x: Number(StartPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 中心点 */
  let MiddlePoint = {
    x: Number((EndPoint.x - StartPoint.x) / 2),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 左点 */
  let RightPoint = {
    x: Number(EndPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 下左点 */
  let BottomLeftPoint = {
    x: Number(EndPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 下中点 */
  let BottomCenterPoint = {
    x: Number(EndPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 下右点 */
  let BottomRightPoint = {
    x: Number(EndPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 位置 */
  let Places = {
    tl: TopLeftPoint,
    tc: TopCenterPoint,
    tr: TopRightPoint,
    l: LeftPoint,
    m: MiddlePoint,
    r: RightPoint,
    bl: BottomLeftPoint,
    bc: BottomCenterPoint,
    br: BottomRightPoint
  }
  return Places;
}

/** 获取线_中点 
 * @参数 StartPoint 开始点    {x,y}
 * @参数 EndPoint   结束点    {x,y}
 * @返回 MiddlePoint   中点   {x,y}
*/
export const onGetLineMiddle = (StartPoint: CoordinateType, EndPoint: CoordinateType): CoordinateType => {
  // console.log("线计算函数-获取线中点");
  let MiddlePoint: CoordinateType = {
    x: 0,
    y: 0
  }
  if (EndPoint.x === StartPoint.x) {
    MiddlePoint = {
      x: Number(StartPoint.x),
      y: Number(StartPoint.y + ((EndPoint.y - StartPoint.y) / 2)),
    }
  }
  else if (EndPoint.y === StartPoint.y) {
    MiddlePoint = {
      x: Number(StartPoint.x + ((EndPoint.x - StartPoint.x) / 2)),
      y: Number(StartPoint.y),
    }
  }
  else {
    MiddlePoint = {
      x: Number(StartPoint.x + ((EndPoint.x - StartPoint.x) / 2)),
      y: Number(StartPoint.y + ((EndPoint.y - StartPoint.y) / 2)),
    }

  }
  return MiddlePoint;
}

/** 获取线_两点间的长度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Lang       长度  
 */
export const onGetLineLang = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  // 两点间的长度
  var Lang = Number(Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)));
  return Lang;
}

/** 获取X轴的夹角_弧度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Radin      弧度
 */
export const onGetLineXRadin = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  /** 弧度 */
  let Radin = Math.atan2((y1 - y2), (x2 - x1));
  return Radin;
};

/** 获取X轴的夹角_角度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Angle      角度
 */
export const onGetLineXAngle = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  /** 弧度 */
  let Radin = Math.atan2((y1 - y2), (x2 - x1));

  return Radin;
}

/** 获取Y轴的夹角-弧度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Radin      弧度
 */
export const onGetLineYRadin = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  /** 弧度 */
  let Radin = Math.atan2((y1 - y2), (x2 - x1));
  return Radin;
}

/** 获取Y轴的夹角-角度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Angle      角度
 */
export const onGetLineYAngle = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  /** 弧度 */
  let Radin = Math.atan2((y1 - y2), (x2 - x1));

  return Radin;
}

/** 获取两点间圆角参数
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 info   中点 [x,y]
 */
export const onGetLineInfo = (StartPoint: CoordinateType, EndPoint: CoordinateType, Angle: number) => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  let PI = Math.PI;
  // 两点间的x轴夹角弧度
  let xAngle = Math.atan2((y2 - y1), (x2 - x1));
  // 转为角度
  xAngle = 360 * xAngle / (2 * PI);
  // 两点间的长度
  let L = Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
  // 计算等腰圆分多边形斜边长度
  let L2 = L / 2 / Math.cos(Angle * 2 * PI / 360);
  // 求第一个顶点坐标，位于下边
  let val1 = [];
  // 求第二个顶点坐标，位于上边
  let val2 = [];
  val1[0] = x1 + Math.round(L2 * Math.cos((xAngle + Angle) * 2 * PI / 360));
  val1[1] = y1 + Math.round(L2 * Math.sin((xAngle + Angle) * 2 * PI / 360));
  val2[0] = x1 + Math.round(L2 * Math.cos((xAngle - Angle) * 2 * PI / 360));
  val2[1] = y1 + Math.round(L2 * Math.sin((xAngle - Angle) * 2 * PI / 360));
  return [val1, val2];
}

/** 获取线路径参数
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 LinePathInfo   中点 [x,y]
 */
export const onGetLinePath = (StartPoint: CoordinateType, EndPoint: CoordinateType): string => {
  let LinePathInfo = `M${StartPoint.x},${StartPoint.y}`;

  return LinePathInfo;
}

/** 求圆周上等分点的坐标
 * @圆心坐标cx
 * @圆心坐标cy
 * @半径r 
 * @等分个数Quantity
 * @返回_Points [{x,y},{x,y,}{x,y}]
*/
export const onGetCircleBisectionPoint = (
  cx: number/*圆心坐标CX*/,
  cy: number/*圆心坐标CY*/,
  radius: number /*半径 */,
  quantity: number /**分段数*/): any => {
  // console.log("✍ 计算函数-求圆周上等分点的坐标");
  // console.log(`cx==${cx};cy==${cy};r==${radius};count==${quantity};`);
  /** 路径点 */
  let pathPoints = [];
  let radians = (Math.PI / 180) * Math.round(360 / quantity); //弧度
  for (let i = 0; i < quantity; i++) {
    let X = cx + radius * Math.sin(radians * i);
    let Y = cy + radius * Math.cos(radians * i);
    pathPoints.push({ x: X, y: Y }); //为保持数据顺时针
  }
  // console.log(pathPoints);
  return pathPoints;
}

function linesIntersect(line1: any, line2: any) {
  // 获取线段1的两个端点
  const x1 = line1[0][0];
  const y1 = line1[0][1];
  const x2 = line1[1][0];
  const y2 = line1[1][1];

  // 获取线段2的两个端点
  const x3 = line2[0][0];
  const y3 = line2[0][1];
  const x4 = line2[1][0];
  const y4 = line2[1][1];

  // 计算向量
  const v1 = [(x4 - x3), (y4 - y3)];
  const v2 = [(x2 - x1), (y2 - y1)];
  const v3 = [(x3 - x1), (y3 - y1)];

  // 计算叉积
  const cross1 = v1[0] * v2[1] - v1[1] * v2[0];
  const cross2 = v3[0] * v2[1] - v3[1] * v2[0];

  // 如果叉积符号相反，则相交
  if (cross1 * cross2 < 0) {
    return true;
  }

  return false;
}
/** 获取两线交点
 * @参数 k1 开始点 x
 * @参数 b1 开始点 y
 * @参数 k2 结束点 x
 * @参数 b2 结束点 y
 * @返回 交点 [x,y]
 */
export const onGetIntersectionPoint = (k1: number, b1: number, k2: number, b2: number): number[] => {
  const x = (b2 - b1) / (k1 - k2)
  const y = k1 * x + b1
  return [x, y];
}

/** 计算矩形差值
 * @参数 x1 开始点 x1
 * @参数 x2 开始点 x2
 * @参数 y1 结束点 y1
 * @参数 y2 结束点 y2
 * @返回  [width,height,top,left]
 */
export const onRectDifference = (x1: number, x2: number, y1: number, y2: number) => {

  /** 差值X */
  let DifferenceX = x2 - x1;
  /** 差值Y */
  let DifferenceY = y2 - y1;
  // console.log(` 差值X==${DifferenceX}\n 差值Y==${DifferenceY}`);

  //★ 计算矩形 长 ；宽 ；
  /*矩形宽度*/
  let width = Math.abs(DifferenceX);
  /*矩形高度*/
  let height = Math.abs(DifferenceY);
  // console.log(` 矩形宽度==${width}\n 矩形高度==${height}`);

  //★ 计算矩形 基点；
  /*基点X*/
  let top = 0;
  if (DifferenceX > 0) {
    top = canvasStore.recordPoint[0].y;
  } else {
    top = canvasStore.recordPoint[1].y;
  }
  /*基点Y*/
  let left = 0;
  if (DifferenceY > 0) {
    left = canvasStore.recordPoint[0].x;
  } else {
    left = canvasStore.recordPoint[1].x;
  }

  return [width, height, top, left]
}

/**———————————————————————————————————————————————————————————————————————————————————————————————— */
//                                ★ 读取Excel文件
/**———————————————————————————————————————————————————————————————————————————————————————————————— */
/**
 * 把文件按照二进制进行读取
 * @param file 
 * @returns 
*/
export const readBinaryFile = (file: File) => {
  // console.log("把文件按照二进制进行读取")
  return new Promise(resolve => {
    let reader = new FileReader()
    reader.readAsBinaryString(file)
    reader.onload = ev => {
      resolve(ev.target?.result);
    }
  })
}

/**
 * 根据sheet索引读取Excel文件中的数据
 * @param file 文件
 * @param sheetIndex excel文件中sheet表索引 默认是从0开始
 * @returns  将表中的数据以json形式的数据返回
*/
export const onReadExcelFile = async (file: File, sheetIndex: number) => {
  // console.log("根据sheet索引读取Excel文件中的数据")
  let data = await readBinaryFile(file);
  let workbook = XLSX.read(data, { type: 'binary' });
  let worksheet = workbook.Sheets[workbook.SheetNames[sheetIndex]];
  data = XLSX.utils.sheet_to_json(worksheet);
  // console.log(data)
  return data
}


